package com.timesheet.ui.utils;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

public class WidgetFactory {

	public static Section createSection(Composite parent, int stye) {
		FormToolkit toolkit = new FormToolkit(Display.getCurrent());
		Section section = toolkit.createSection(parent, stye);
		return section;
	}

	public static Section createSectionWithToolbar(Composite parent, int style) {
		ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
		Section section = createSection(parent, style);
		ToolBar toolbar = toolBarManager.createControl(section);

		Action action1 = new Action("A1") {

			public void run() {
				System.out.println("A1");
			}
		};
		toolBarManager.add(action1);
		toolBarManager.update(true);
		section.setTextClient(toolbar);

		return section;
	}

	public static ToolBarManager createToolbarManager(Section section) {
		ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
		section.setTextClient(toolBarManager.createControl(section));
		return toolBarManager;
	}

	public static void addActions2Section(ToolBarManager toolBarManager, IAction... actions) {

		for (IAction iAction : actions) {
			toolBarManager.add(iAction);
			toolBarManager.update(true);
		}

	}

	public static void addAction(Section section, IAction action) {

		Control[] children = section.getChildren();
		for (Control control : children) {
			System.out.println(control);
		}

	}

	/**
	 * create a toolbar in the passed section
	 * 
	 * @param section
	 * @param toolBarManager
	 */
	protected void createSectionToolbar(Section section, ToolBarManager toolBarManager) {
		FormToolkit toolkit = new FormToolkit(Display.getCurrent());
		Composite toolbarComposite = toolkit.createComposite(section);
		toolbarComposite.setBackground(null);
		toolBarManager.createControl(toolbarComposite);
		section.clientVerticalSpacing = 0;
		section.descriptionVerticalSpacing = 0;
		section.setTextClient(toolbarComposite);
	}

}
